package com.teyao.teyao_back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teyao.teyao_back.entity.Product;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @PostMapping("/updateSto")
    public ResponseMessage updateSto(Integer productId, Integer finishQuantity) {
        Product product = new Product();
        product.setProductId(productId);
        product.setStockCount(finishQuantity);
        return productService.updateSto(product);
    }

    @GetMapping("/all")
    public List<Product> all() {
        return productService.all();

    }


    @PostMapping("/deleteById")
    public ResponseMessage updateSto(Integer productId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setSuccess(productService.remove(queryWrapper));
        return responseMessage;
    }

    @PostMapping("/insertNew")
    public ResponseMessage insertNew(Product product) {
        System.out.println(product);
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setSuccess(productService.save(product));
        return responseMessage;
    }

    //库存预警
    @GetMapping("/stockAlert")
    public ResponseMessage stockAlert() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("stock_count", 300);
        responseMessage.setData(productService.list(queryWrapper));
        return responseMessage;
    }
}
